#include <stdio.h>
#include <stdlib.h>
#include "coro.h"
#include "fs_walk.h"
#include <string.h>

void usage()
{
    puts("find -name file-name dir");
}

char *aim;
char *dir_path;
//extern void fs_walk();

coro_t *coro_d;
coro_t *coro_f;

void find_file()
{
	while(1) {
		char *path = (char *)coro_resume(coro_d);
		if(path == NULL) break;
		
		FILE *file = fopen(path, "r");

		char line[256];
		while (fgets(line, sizeof(line), file)) {
			if(strstr(line, aim))
				printf("%s: %s", path, line);
		}
        	fclose(file);
	}
    	exit(0);
}

int main(int argc, char *argv[])
{
    /*aim = malloc(strlen(argv[2]));
    strcpy(aim, argv[2]);
    path = malloc(strlen(argv[1]));
    strcpy(path, argv[1]);*/
    
    aim = argv[2];
    dir_path = argv[1];
    
    coro_d = coro_new(fs_walk);
    coro_f = coro_new(find_file);

    printf("BEGIN\n");
    coro_boot(coro_f);
    printf("END\n");

    return 0;
}
